{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "Chapter 05\n",
    "# 字符串索引和切片\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "字符串的长度为：\n",
      "11\n"
     ]
    }
   ],
   "source": [
    "greeting_str = 'Hey, James!'\n",
    "# 打印字符串长度\n",
    "print('字符串的长度为：')\n",
    "print(len(greeting_str))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T15:52:20.956900Z",
     "end_time": "2024-07-02T15:52:20.957901Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "字符：H，索引：0\n",
      "字符：e，索引：1\n",
      "字符：y，索引：2\n",
      "字符：,，索引：3\n",
      "字符： ，索引：4\n",
      "字符：J，索引：5\n",
      "字符：a，索引：6\n",
      "字符：m，索引：7\n",
      "字符：e，索引：8\n",
      "字符：s，索引：9\n",
      "字符：!，索引：10\n"
     ]
    }
   ],
   "source": [
    "# 打印每个字符和对应的索引\n",
    "for index, char in enumerate(greeting_str):\n",
    "    print(f\"字符：{char}，索引：{index}\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T15:52:39.851140Z",
     "end_time": "2024-07-02T15:52:39.851140Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "H\n",
      "e\n"
     ]
    }
   ],
   "source": [
    "# 单个字符索引\n",
    "print(greeting_str[0])\n",
    "print(greeting_str[1])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T15:52:54.807609Z",
     "end_time": "2024-07-02T15:52:54.808605Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "!\n",
      "s\n"
     ]
    }
   ],
   "source": [
    "print(greeting_str[-1])\n",
    "print(greeting_str[-2])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T15:53:03.113646Z",
     "end_time": "2024-07-02T15:53:03.114637Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hey\n"
     ]
    }
   ],
   "source": [
    "# 切片\n",
    "# 取出前3个字符，索引为0、1、2\n",
    "print(greeting_str[:3])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T15:53:10.600591Z",
     "end_time": "2024-07-02T15:53:10.600591Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ey, J\n"
     ]
    }
   ],
   "source": [
    "# 取出索引1、2、3、4、5，不含0，不含6\n",
    "print(greeting_str[1:6])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T15:53:17.053312Z",
     "end_time": "2024-07-02T15:53:17.053312Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hy ae!\n"
     ]
    }
   ],
   "source": [
    "# 指定步长2，取出第0、2、4 ...\n",
    "print(greeting_str[::2])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T15:53:28.585821Z",
     "end_time": "2024-07-02T15:53:28.585821Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "!semaJ ,yeH\n"
     ]
    }
   ],
   "source": [
    "# 指定步长-1，倒序\n",
    "print(greeting_str[::-1])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T15:53:35.853237Z",
     "end_time": "2024-07-02T15:53:35.853237Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
